package com.example.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.model.domain.Room;
import com.example.mybatisplus.mapper.RoomMapper;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.model.dto.BedInfoDTO;
import com.example.mybatisplus.service.RoomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxp
 * @since 2024-07-17
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Override
    public Page<Room> pageList(Room room, PageDTO dto) {
        QueryWrapper<Room> wrapper =new QueryWrapper<>();

        if(Objects.nonNull(room.getRoomId())){
            wrapper.eq("roomId",room.getRoomId());
        }
        Page<Room> page = new Page<>();
        page.setCurrent(dto.getPageNo()).setSize(dto.getPageSize());
        baseMapper.selectPage(page,wrapper);

        return page;
    }

    @Override
    public boolean removeRoom(Room room) {
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("roomId",room.getRoomId());
        Room one = baseMapper.selectOne(wrapper);
        if(one.getBedNum() == 0){ // 该房间可被直接删除
            baseMapper.deleteById(one.getRoomId());
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean forceRemoveRoom(Room room) {
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("roomId",room.getRoomId());
        Room one = baseMapper.selectOne(wrapper);
        one.setBedNum(0);
        baseMapper.updateById(one);
        baseMapper.deleteById(one.getRoomId());
        return true;
    }

}
